//处理字典对象
zUtil.dictConvertHandler = function(value,type,dictData){
    var oldValue = value;
    if(value && 'undefined' !== $.type(type) && type){
        if('string' === $.type(type) || 'object' === $.type(type)){//字典取或者key-value形式
            var dicts = ('object' === $.type(type)?type:dictData[type]);
            var getDesc = function(value){
                var nlabel = dicts[value];
                if('object' === $.type(nlabel)){
                  if(nlabel["labelCode"]){
                      nlabel = nlabel["labelCode"];
                  }else if(nlabel["text"]){
                      nlabel = nlabel["text"];
                  }else if(nlabel["title"]){
                      nlabel = nlabel["title"];
                  }else{//自动解析title
                      $.each(nlabel,function(k,v){
                          if(k.toLowerCase().indexOf("title")!=-1 || k.toLowerCase().indexOf("display")!=-1){
                              nlabel = v;
                              return false;
                          }
                      });
                  }
                }
                return nlabel;
            };
            if('undefined' !== $.type(dicts) && 'undefined' !== $.type(dicts[value]) && dicts[value] !== null){
                value = getDesc(value);
            }else if('string' === $.type(value) && value.indexOf(",") != -1){
                var arrval = value.split(",");
                var desc = [];
                $.each(arrval,function(i, val){
                    var nlabel = getDesc(val);
                    if(nlabel){
                       desc.push(nlabel);
                    }else{
                       desc.push(val);
                    }
                });
                if(desc.length>0){
                    value = desc.join(","); 
                }
            }
        }
      }
      if(!value){
          value = oldValue;
      }
      return value;
}
//请求字典
zUtil.dictRequestFunc = function(dictobj, url1, url2, type, keyValue, syncFlag,func){
    if('undefined' === $.type(keyValue)){
      keyValue = null;
    }
    var selectArr = type.split(",");
    var type1 = [];
    var type2 = [];
    $.each(selectArr,function(i,item){
        if(item.indexOf("ZSELECT_") > -1){
            type2.push(item);
        }else{
            type1.push(item);
        }
    });
    var resultHandler= function(result){
        if(zCode["GLOBAL_SUCCESS"] === zUtil.getVal(result,zConst["busCode"]) && result[zConst["resData"]] !== null){
            dictobj = $.extend(true,dictobj,result[zConst["resData"]]);
            if(func){
                func(dictobj);
            }
        }
    };
    
    if(type1.length > 0 && url1){
        if(syncFlag){
            var result = zUtil.ajaxPostSync(url1,{reqData:{type: type1.join(","), key:keyValue}});
            resultHandler(result);
        }else {
            zUtil.ajaxPost(url1,{reqData:{type:type}},resultHandler);
        }
    }
    if(type2.length > 0 && url2){
        if(syncFlag){
            var result = zUtil.ajaxPostSync(url2,{reqData:{type: type2.join(","), key:keyValue}});
            resultHandler(result);
        }else {
            zUtil.ajaxPost(url2,{reqData:{type:type}},resultHandler);
        }
    }
};

/** zDict字典按TYPE转换CODE*/
zUtil.dictConvertOfSys = function(value,type){
    return zUtil.dictConvertHandler(value, type, zDictSys);
};
/** zAutocodeDict字典按TYPE转换CODE*/
zUtil.dictConvertOfAutocode = function(value,type){
    return zUtil.dictConvertHandler(value, type, zDictAutocode);
}
/** zDesignDict字典按TYPE转换CODE*/
zUtil.dictConvertOfDesign = function(value,type){
    return zUtil.dictConvertHandler(value, type, zDictDesign);
}

/**字典装配字典-同步,type:逗号分割*/
zUtil.dictLoadSyncOfSys = function(type,keyValue){
    zUtil.dictRequestFunc(zDictSys,zApi["sysDictCode"],zApi["sysDictCode"],type,keyValue,true);;
};
/**字典装配字典-同步,type:逗号分割*/
zUtil.dictLoadSyncOfAutocode = function(type,keyValue){
    zUtil.dictRequestFunc(zDictAutocode,zApi["autocodeDictCode"],zApi["autocodeSelectCode"],type,keyValue,true);;
};
/**字典装配-异步*/
zUtil.dictLoadOfAutocode = function(type,keyValue,func){
    zUtil.dictRequestFunc(zDictAutocode,zApi["autocodeDictCode"],zApi["autocodeSelectCode"],type,keyValue,false,func);
};

/**字典装配字典-同步,type:逗号分割*/
zUtil.dictLoadSyncOfDesign = function(type,keyValue){
    zUtil.dictRequestFunc(zDictDesign,zApi["designDictCode"],zApi["designSelectCode"],type,keyValue,true);
};
zUtil.dictLoadOfDesign = function(type,keyValue,func){
    zUtil.dictRequestFunc(zDictDesign,zApi["designDictCode"],zApi["designSelectCode"],type,keyValue,false,func);
};
